package com.kmxd.ams.client.event.log;

import cn.hutool.system.SystemUtil;
import com.kmxd.ams.client.event.AmsEvent;
import com.kmxd.ams.core.util.IpUtil;
import java.net.InetAddress;
import java.util.Date;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

/**
 * 文档日志
 *
 * @author nickbi
 */
@Slf4j
@EqualsAndHashCode(callSuper = true)
@Getter
@ToString(callSuper = true)
public class LoginLogEvent extends AmsEvent {
  private Long comId;

  /** 操作类型 */
  private final String optType;

  /** 登录状态，1成功，2登录失败,3锁定中，4非工作时间，5其他 */
  private final Integer opStatus;

  /** 操作时间 */
  private final Date opTime;

  /** 登录人IP */
  private String ip;

  private Long userId;

  /** 登录人代码 */
  private String userCode;

  /** 客户端 */
  private String client;

  /** 登录方式：账号密码登录，SSO登录、短信登录、直接退出、超时退出、重复登录退出等； */
  private final String loginMode;

  /** 日志安全等级：0低，1中，2高，3高危 */
  private Integer level;

  /** 备注 */
  private final String remarks;

  /** Mac地址 */
  private String macaddr;

  public LoginLogEvent(
      String optType, Integer opStatus, String userCode, String loginMode, String remarks) {
    this.optType = optType;
    this.opStatus = opStatus;
    this.opTime = new Date();
    this.userCode = userCode;
    this.loginMode = loginMode;
    this.remarks = remarks;
    // 获取本地IP对象
    try {
      InetAddress ia1 = InetAddress.getLocalHost();
      this.macaddr = IpUtil.getMACAddress(ia1);
      this.client = SystemUtil.getOsInfo().getName();
      this.ip = IpUtil.getLocalIP();
    } catch (Exception e) {
      log.error(e.getMessage(), e);
    }
  }

  public LoginLogEvent(
      String optType, Integer opStatus, Long comId, Long userId, String loginMode, String remarks) {
    this.optType = optType;
    this.opStatus = opStatus;
    this.opTime = new Date();
    this.userId = userId;
    this.comId = comId;
    this.loginMode = loginMode;
    this.remarks = remarks;
    // 获取本地IP对象
    try {
      InetAddress ia1 = InetAddress.getLocalHost();
      this.macaddr = IpUtil.getMACAddress(ia1);
      this.client = SystemUtil.getOsInfo().getName();
      this.ip = IpUtil.getLocalIP();
    } catch (Exception e) {
      log.error(e.getMessage(), e);
    }
  }
}
